<?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace app\common\traits\controller;

trait Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return mixed
     * @author : Randy_chen
     * @Date   : 2019/7/13
     * @Time   : 10:57
     */
    public function index()
    {
        if ($this->request->post()) {
                $res = $this->_query($this->table)
                    ->order('id desc')
                     ->page(true, false ,'',request()->param('limit'));
            $res['data'] = $res['list'];
            $this->success('查询成功!', $res);
        } else {
            $this->applyCsrfToken();
            return $this->fetch();
        }
    }

    /**
     *修改
     * @auth true
     * @author : Randy_chen
     * @Date   : 2019/7/13
     * @Time   : 10:58
     */
    public function edit()
    {
        $this->isAddMode = '0';
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 添加.
     * @auth true
     *
     * @return mixed
     */
    public function add()
    {
        $this->isAddMode = '1';
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 删除.
     * @auth true
     * @return mixed
     */
    public function del()
    {
        $this->_delete($this->table);
    }


    /**
     * 列表数据处理.
     *
     * @param array $data
     *
     * @throws \Exception
     */
    protected function _index_page_filter(&$data)
    {

    }

    /**
     * 删除结果处理.
     *
     * @param bool $result
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    protected function _del_delete_result($result)
    {
    }


    /**
     * 表单数据处理.
     *
     * @param array $data
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _form_filter(&$data)
    {

    }

    /**
     * 表单结果处理.
     *
     * @param bool $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('编辑成功！', 'javascript:history.back()');
        }
    }
}
